﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        string out1= "Out_1Hz(", out2 ="Out_2Hz(" ,out4 = "Out_4Hz(", out8 = "Out_8Hz(", out16 = "Out_16Hz(" , end_str = ")\r\n";
        public Form1()
        {
            InitializeComponent();
        }
        private void SearchAndAddSerialToComboBox(SerialPort MyPort, ComboBox MyBox)
        {                                                               //将可用端口号添加到ComboBox
            string[] MyString = new string[20];                         //最多容纳20个，太多会影响调试效率
            string Buffer;                                              //缓存
            MyBox.Items.Clear();                                        //清空ComboBox内容
            for (int i = 1; i < 20; i++)                                //循环
            {
                try                                                     //核心原理是依靠try和catch完成遍历
                {
                    Buffer = "COM" + i.ToString();
                    MyPort.PortName = Buffer;
                    MyPort.Open();
                    MyString[i - 1] = Buffer;
                    MyBox.Items.Add(Buffer);
                    MyPort.Close();
                }
                catch
                {

                }
            }
            MyBox.Text = MyString[0];
        }

        private void WriteByteToSerialPort(byte data)                   //单字节写入串口
        {
            byte[] Buffer = new byte[2] { 0x00, data };                       //定义数组
            if (serialPort1.IsOpen)                                     //传输数据的前提是端口已打开
            {
                try
                {
                    serialPort1.Write(Buffer, 0, 2);                    //写数据
                }
                catch
                {
                    MessageBox.Show("串口数据发送出错，请检查.", "错误");//错误处理
                }
            }
        }



        private void button1_Click(object sender, EventArgs e)
        {
            SearchAndAddSerialToComboBox(serialPort1, comboBox1);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SearchAndAddSerialToComboBox(serialPort1, comboBox1);
            numericUpDown1.Value = 5;
            //comboBox2.Text = "115200";
            //serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);//必须手动添加事件处理程序
        }
        //private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)//串口数据接收事件
        //{

        //    byte data;
        //    data = (byte)serialPort1.ReadByte();//此处需要强制类型转换，将(int)类型数据转换为(byte类型数据，不必考虑是否会丢失数据
        //    string str = Convert.ToString(data, 16).ToUpper();//转换为大写十六进制字符串
        //    //string str = serialPort1.ReadExisting();//字符串方式读
        //    textBox1.AppendText(str);//添加内容
        //}
        //private void WriteByteToSerialPort(byte data)                   //单字节写入串口
        //{
        //    byte[] Buffer = new byte[2] { 0x00, data };                       //定义数组
        //    if (serialPort1.IsOpen)                                     //传输数据的前提是端口已打开
        //    {
        //        try
        //        {
        //            serialPort1.Write(Buffer, 0, 2);                    //写数据
        //        }
        //        catch
        //        {
        //            MessageBox.Show("串口数据发送出错，请检查.", "错误");//错误处理
        //        }
        //    }
        //}

        private void button3_Click(object sender, EventArgs e)
        {
   
            serialPort1.WriteLine(out8 + numericUpDown1.Value+end_str);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine(out16 + numericUpDown1.Value + end_str);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Out1_2Hz("+ numericUpDown1.Value + end_str);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Out1_8Hz(" + numericUpDown1.Value + end_str);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Fir(0,0,0,0)\r\n");
        }

        private void button18_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Fir(1,1,0,1)\r\n" );
        }

        private void button13_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Fir(1,0,0,0)\r\n");
        }

        private void button14_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Fir(1,1,0,0)\r\n");
        }

        private void button15_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Fir(1,0,1,0)\r\n");
        }

        private void button16_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Fir(1,1,1,0)\r\n");
        }

        private void button17_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Fir(1,0,0,1)\r\n");
        }

        private void button19_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Fir(1,0,1,1)\r\n" );
        }

        private void button20_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Fir(1,1,1,1)\r\n");
        }

        private void button11_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Out1_16Hz(" + numericUpDown1.Value + end_str);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Out1_4Hz(" + numericUpDown1.Value + end_str);
        }

        private void button21_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Sec(0,0,0,0)\r\n");
        }

        private void button22_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Sec(1,0,0,0)\r\n");
        }

        private void button23_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Sec(1,1,0,0)\r\n");
        }

        private void button24_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Sec(1,0,1,0)\r\n");
        }

        private void button25_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Sec(1,1,1,0)\r\n");
        }

        private void button26_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Sec(1,0,0,1)\r\n");
        }

        private void button27_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Sec(1,1,0,1)\r\n");
        }

        private void button28_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Sec(1,0,1,1)\r\n");
        }

        private void button29_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine("Sec(1,1,1,1)\r\n");
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine(out1+ numericUpDown1.Value+ end_str);
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine(out2 + numericUpDown1.Value+end_str);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine(out4 + numericUpDown1.Value+end_str);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)                                     //串口打开就关闭
            {
                try
                {
                    serialPort1.Close();
                }
                catch { }                                               //确保万无一失
                button2.Text = "打开串口";

            }
            else
            {
                try
                {
                    serialPort1.PortName = comboBox1.Text;              //端口号
                    serialPort1.Open();                                 //打开端
                    button2.Text = "关闭串口";
                }
                catch
                {
                    MessageBox.Show("串口打开失败", "错误");
                }
            }
        }
    }
}
